# Models for GLES

# JAGS sampler for the four vignett descions in the GLES

# Required Packages
library(rjags)

# Load Data
load("gles.Rdata")

# Additional functions
source("Model_Data_gles.R")


# Define quantaties
iter <-  2000
adapt <- 18000
di <- 500
N <- NULL


######
# Analyse mixing in Vignett Decision
# A) CDU-Gruene


dta <- create.data(parties=c("cdu","gruene"),vignette="a",coal="f")

jags.dta <- list(
                   "UC"=dta[,grep("ratcoal",colnames(dta))]
                 , "UP"=dta[,grep("rat_",colnames(dta))]
                 , "lr" = dta[,grep("lr",colnames(dta))]
                 , "Y"=dta[,1]
                 ,"a11" = as.numeric(dta[,grep("a1",names(dta))]==1)
                 ,"a12" = as.numeric(dta[,grep("a1",names(dta))]==2)
                 ,"pid"  = dta[,grep("pid",colnames(dta))]
                 ,"N"=nrow(dta)
                 ,"X"=dta[,grep("con_",names(dta))]
                 )

N[1] <- jags.dta$N

model <- jags.model(file="Model_gles.bug", jags.dta,n.adapt=adapt,n.chains=2)
cdu.gruene.rat <- coda.samples(model,c("diff","gamma","beta","delta","psi"),iter)
cdu.gruene.rat.dic <- dic.samples(model,di)
print(summary(cdu.gruene.rat))



######
# Analyse mixing in Vignett Decision
# B) SPD-Gruene-FDP


dta <- create.data(parties=c("spd","gruene","fdp"),vignette="b",coal="i")

jags.dta <- list(
                 "UC"=dta[,grep("ratcoal",colnames(dta))]
                 , "UP"=dta[,grep("rat_",colnames(dta))]
                 , "lr" = dta[,grep("lr",colnames(dta))]
                 , "Y"=dta[,1]
                 ,"a11" = as.numeric(dta[,grep("a1",names(dta))]==1)
                 ,"a12" = as.numeric(dta[,grep("a1",names(dta))]==2)
                 ,"a13" = as.numeric(dta[,grep("a1",names(dta))]==3)
                 ,"pid"  = dta[,grep("pid",colnames(dta))]
                 ,"X"=dta[,grep("con_",names(dta))]
                 ,"N"=nrow(dta)
                 )

N[2] <- jags.dta$N

model <- jags.model(file="Model_gles2.bug", jags.dta,n.adapt=adapt,n.chains=2)


spd.gruene.fdp.rat <- coda.samples(model,c("diff","gamma","beta","delta","psi"),iter)
spd.gruene.fdp.rat.dic <- dic.samples(model,di)
print(summary(spd.gruene.fdp.rat))


#####
# Analyse mixing in Vignett Decision 
# C) SPD - LINKE - Gruene

dta <- create.data(parties=c("spd","gruene","linke"),vignette="c",coal="j")

jags.dta <- list(
                 "UC"=dta[,grep("ratcoal",colnames(dta))]
                 , "UP"=dta[,grep("rat_",colnames(dta))]
                 , "lr" = dta[,grep("lr",colnames(dta))]
                 , "Y"=dta[,1]
                 ,"a11" = as.numeric(dta[,grep("a1",names(dta))]==1)
                 ,"a12" = as.numeric(dta[,grep("a1",names(dta))]==2)
                 ,"a13" = as.numeric(dta[,grep("a1",names(dta))]==3)
                 ,"pid"  = dta[,grep("pid",colnames(dta))]
                 ,"X"=dta[,grep("con_",names(dta))]
                 ,"N"=nrow(dta)
                 )


N[3] <- jags.dta$N

model <- jags.model(file="Model_gles2.bug", jags.dta,n.adapt=adapt,n.chains=2)

spd.gruene.linke.rat <- coda.samples(model,c("diff","gamma","beta","delta","psi"),iter)
spd.gruene.linke.rat.dic <- dic.samples(model,di)
print(summary(spd.gruene.linke.rat))




#######
# Analyse mixing Vignette decion
# D) SPD-FDP

dta <- create.data(parties=c("spd","fdp"),vignette="d",coal="h")

jags.dta <- list(
                   "UC"=dta[,grep("ratcoal",colnames(dta))]
                 , "UP"=dta[,grep("rat_",colnames(dta))]
                 , "lr" = dta[,grep("lr",colnames(dta))]
                 , "Y"=dta[,1]
                 ,"a11" = as.numeric(dta[,grep("a1",names(dta))]==1)
                 ,"a12" = as.numeric(dta[,grep("a1",names(dta))]==2)
                 ,"pid"  = dta[,grep("pid",colnames(dta))]
                 ,"N"=nrow(dta)
                 ,"X"=dta[,grep("con_",names(dta))]
                 )

N[4] <- jags.dta$N

model <- jags.model(file="Model_gles.bug", jags.dta,n.adapt=adapt,n.chains=2)
spd.fdp.rat <- coda.samples(model,c("diff","gamma","beta","delta","psi"),iter)
spd.fdp.rat.dic <- dic.samples(model,di)
print(summary(spd.fdp.rat))



#### Save Models

save(
      cdu.gruene.rat ,cdu.gruene.rat.dic
     ,spd.gruene.fdp.rat, spd.gruene.fdp.rat.dic
     ,spd.gruene.linke.rat, spd.gruene.linke.rat.dic
     ,spd.fdp.rat,spd.fdp.rat.dic
     ,N
    , file= "models_gles_rating.Rdata")
